home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_08 / gottner / test.cpp < prev   
Encoding:
C/C++ Source or Header  |  1995-06-04  |  1.3 KB  |  68 lines

  1. /*--------------------------------------------------------*
  2.  * Simple program to test hashing.
  3.  *
  4.  * Problem: Read a list of words from stdin, count the
  5.  *          frequency of each word and each letter.
  6.  *--------------------------------------------------------*/
  7.  
  8. #include <iostream.h>
  9. #include <iomanip.h>
  10. #include <cstring.h>
  11. #include "hashmap.h"
  12.  
  13. static unsigned
  14. hash_str(const string &str)
  15. {
  16.     return str.hash();
  17. }
  18.  
  19.  
  20. static unsigned
  21. hash_char(const char &theChar)
  22. {
  23.     return theChar;
  24. }
  25.  
  26.  
  27. static void
  28. print_word(const string &word,
  29.            int &count,
  30.            void *out)
  31. {
  32.     (* (ostream *) out) << word << '\t' << count << endl;
  33. }
  34.  
  35.  
  36. static void
  37. print_char(const char &ch, int &count, void *out)
  38. {
  39.     (* (ostream *) out) << ch << '\t' << count << endl;
  40. }
  41.  
  42.  
  43.  
  44. int
  45. main(int, char **)
  46. {
  47.     Map<string, int>  words(hash_str, 0);
  48.     Map<char, int>    letters(hash_char, 0);
  49.  
  50.     string str;
  51.  
  52.     while (cin >> str) {
  53.         ++words[str];
  54.         for (int i = 0; i < str.length(); ++i)
  55.             ++letters[str[i]];
  56.     }
  57.  
  58.     cout << "\t\tWords:" << endl << endl;
  59.     words.apply(print_word, &cout);
  60.     cout << endl;
  61.  
  62.     cout << "\t\tLetters:" << endl << endl;
  63.     letters.apply(print_char, &cout);
  64.     cout << endl;
  65.  
  66.     return 0;
  67. }
  68.